package com.leedsoft.common;

import com.leedsoft.controller.dto.AidMemDto;
import com.leedsoft.controller.dto.NodeDto;
import com.leedsoft.domain.SysRscDict;
import com.leedsoft.domain.SysRscDictExample;
import com.leedsoft.mapper.SysRscDictMapper;
import com.leedsoft.service.AidService;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Created by leeds on 2017/6/20.
 */
public class DictUtil {
    private static SysRscDictMapper dictMapper = SpringContextHolder.getBean(SysRscDictMapper.class);
    private static AidService aidService = SpringContextHolder.getBean(AidService.class);
    private static final int SIZE = 50;
    private static LruCache<String, List<SysRscDict>> cache = new LruCache<>(SIZE);

    public static List<SysRscDict> getSubDictsByCode(String code) {
        Assert.notNull(code, "数据字典code不可为空");
        List<SysRscDict> res = cache.get(code);
        if (res != null) return res;

        SysRscDictExample e = new SysRscDictExample();
        e.createCriteria().andCODEEqualTo(code);
        List<SysRscDict> dicts = dictMapper.selectByExample(e);
        Assert.notEmpty(dicts, "code所指向的数据字典不存在");
        SysRscDict dict = dicts.get(0);

        e.clear();
        e.setOrderByClause("SORT_NUM ASC");
        e.createCriteria().andPARENT_IDEqualTo(dict.getID());
        res = dictMapper.selectByExample(e);
        cache.put(code, res);
        return res;
    }

    public static List<AidMemDto> getAidMems(Long planId) {
        // TODO: 2017/6/30 此处应有缓存。。设计一个5分钟缓存。
        return aidService.getAidMems(planId);
    }

    public static List<NodeDto> getNoOutNodes(Long planId) {
        return aidService.getNoOutNodes(planId);
    }

    public static List<NodeDto> getExistNodes(Long planId) {
        return aidService.getExistNodes(planId);
    }




}
